package com.bjpowernode.web;

import com.bjpowernode.entity.TPermission;
import com.bjpowernode.entity.TUser;
import com.bjpowernode.entity.TUserDO;
import com.bjpowernode.query.TUserQuery;
import com.bjpowernode.result.R;
import com.bjpowernode.service.TpermissionService;
import com.bjpowernode.service.UserService;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;


/**
 *
 */
@RestController
@Slf4j
public class UserController {
    @Resource
    private UserService userService;

    @Resource
    private TpermissionService tpermissionService;

    @RequestMapping("/api/userInfo")
    public R userInfo(Authentication authentication) {
        return R.OK(authentication);
    }

    @RequestMapping("/api/autoLogin")
    public R autoLogin() {
        return R.OK();
    }

    @GetMapping("/api/user")
    public R userByPage(@RequestParam(value = "current", defaultValue = "1") Integer current) {
        log.info("current : {}", current);
        PageInfo<TUser> userPageInfo = userService.getUserByPage(current);
        log.info("值 : " + userPageInfo.getPageSize());
        return R.OK(userPageInfo);
    }

    @PostMapping("/api/user")
    public R save(@Validated TUserQuery userQuery, @RequestHeader(value = "Authorization") String jwt) {
        log.info("登录 , 数据为 : {}", userQuery);
        userQuery.setJwt(jwt);
        boolean save = userService.save(userQuery);
        return R.OK();
    }

    @DeleteMapping("/api/user/{id}")
    public R remove(@PathVariable Integer id) {
        log.info("要删除的用户id : {}", id);
        boolean save = userService.removeUserById(id);
        return R.OK();
    }

    @DeleteMapping("/api/user")
    public R batchDelete(@RequestParam(value = "ids") String ids) {
        log.info("要批量删除的用户ids : {}", ids);
        // 将数据转换回集合
        String[] idsStringArray = ids.split(",");
        List<Integer> idList = Arrays.stream(idsStringArray)
                .map(Integer::parseInt)
                .collect(Collectors.toList());
        int batched = userService.batchDeleteUsersByIds(idList);
        return batched > 0 ? R.OK() : R.FAIL();
    }

    @GetMapping("/api/user/{id}")
    public R getUserById(@PathVariable Integer id) {
        log.info("要获取的用户id : {}", id);
        TUser tUser = userService.getUserById(id);
        return R.OK(tUser);
    }

    @PutMapping("/api/user")
    public R updateUser(@Validated TUserQuery tUserQuery,
                        @RequestHeader(value = "Authorization") String jwt) {
        log.info("要修改的用户信息 : {}", tUserQuery);
        // 修改实现
        tUserQuery.setJwt(jwt);

        int count = userService.updataUserById(tUserQuery);
        return count >= 1 ? R.OK() : R.FAIL();
    }

    @GetMapping("/api/user/owner")
    public R userOwner() {
        log.info("获取所有用户名 和 id");
        // 查缓存 , 没有再查数据库 , 并存入数据库

        List<TUserDO> tUserList = userService.findAllUser();
        return R.OK(tUserList);
    }

    /**
     * 加载当前用户 对应的菜单列表  --- 权限菜单
     * @param userId
     * @return
     */
    @GetMapping("/api/user/{userId}/menu")
    public R loadCurrentUserMenu(@PathVariable(value = "userId") Integer userId) {
        log.info("要加载当前用户菜单的 用户id为 : {}" , userId);
        List<TPermission> tPermissions = tpermissionService.selectMenuPermissionByUserId(userId);
        return R.OK(tPermissions);
    }

}
